package com.cyandpos.pad.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.cyandpos.pad.R;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.view.InputNumberDialogCommon;

public  class  InputNumberDialog  extends AlertDialog {
    private Context context;
    private View view;
    private TextView tv_title;
    private EditText inputnumber_edtext;
    private SelfNumKeyView inputnumber_keyboard;
    private InputNumberDialog.InputDialogOnSubmitClickListener inputDialogOnSubmitClickListener;
    public  static  InputNumberDialog inputNumberDialog;

    protected InputNumberDialog(Context context ,final InputNumberDialog.InputDialogOnSubmitClickListener inputDialogOnSubmitClickListener) {
        super(context);
        this.context=context;
        this.inputDialogOnSubmitClickListener=inputDialogOnSubmitClickListener;
        view = LayoutInflater.from(context).inflate(R.layout.dialog_inputnumber,null);

        tv_title=view.findViewById(R.id.tv_dialog_title_name);
        inputnumber_edtext=view.findViewById(R.id.dialog_inputnumber_edtext);
        inputnumber_keyboard=view.findViewById(R.id.dialog_inputnumber_keyboard);

        inputnumber_keyboard.setEnterReceptor(inputnumber_edtext);
        inputnumber_keyboard.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
                if (inputDialogOnSubmitClickListener != null) {
                    inputDialogOnSubmitClickListener.InputDialogOnSubmitClick(num);
                    inputNumberDialog.dismiss();
                    inputNumberDialog=null;
                }


            }
        });


          CommonUtils.setNoSoftware(inputnumber_edtext, (Activity) context, true);

        tv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputNumberDialog.dismiss();
                inputNumberDialog=null;
            }
        });

    }
    public static  InputNumberDialog getInputNumberDialog(Context context, InputNumberDialog.InputDialogOnSubmitClickListener inputDialogOnSubmitClickListener){
        if(inputNumberDialog==null){
            inputNumberDialog=new InputNumberDialog(context,inputDialogOnSubmitClickListener);
        }
        return inputNumberDialog;
    }

    /**
     * 是否允许输入小数点
     * @param
     */
    public void setIsCanInputPoint(boolean isCanInputPoint){
        inputnumber_keyboard.setIsCanInputPoint(isCanInputPoint);
    }


    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        tv_title.setText(title);
    }

    /**
     * 设置输入框描述内容
     * @param hint
     */
    public void setEtHint(String hint){
        inputnumber_edtext.setHint(hint);
    }

    public interface InputDialogOnSubmitClickListener{
        void InputDialogOnSubmitClick(String content);
    }

    /**
     * 显示弹窗
     *
     * @param isClose
     */
    public  void showDialog(boolean isClose){
        inputNumberDialog.setCancelable(isClose);//当设置为flase，点击返回键是无法返回的。默认为true
        inputNumberDialog.setView(view,0,0,0,0);
        inputNumberDialog.show();
        inputNumberDialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg_common);
    }


}

